FROM nginx:1.21.6

ADD ./ngx_http_vhost_traffic_status_module.so /etc/nginx/modules
ADD ./echo_server   /root
ADD ./run_8000-2.sh /root

RUN set -x \
# load_module ngx_http_vhost_traffic_status_module
    && sed -i "/events {/iload_module modules/ngx_http_vhost_traffic_status_module.so;" /etc/nginx/nginx.conf \
# vhost_traffic_status
    && sed -i "/server {/ivhost_traffic_status_zone shared:vhost_traffic_status:32m;\n" /etc/nginx/conf.d/default.conf \
# cache
    && sed -i "/server {/iproxy_cache_path /var/cache/nginx levels=1:2 keys_zone=custom_cache:10m inactive=60m;\n" /etc/nginx/conf.d/default.conf \
# location /status
    && sed -i "/location \/ {/i\    location \/status {" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        vhost_traffic_status_display;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        vhost_traffic_status_display_format html;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\    }\n" /etc/nginx/conf.d/default.conf \
# proxy1
    && sed -i "/server {/i\upstream proxy1 {" /etc/nginx/conf.d/default.conf \
    && sed -i "/server {/i\    server localhost:8000;" /etc/nginx/conf.d/default.conf \
    && sed -i "/server {/i\}\n" /etc/nginx/conf.d/default.conf \
# proxy2
    && sed -i "/server {/i\upstream proxy2 {" /etc/nginx/conf.d/default.conf \
    && sed -i "/server {/i\    server localhost:8001;" /etc/nginx/conf.d/default.conf \
    && sed -i "/server {/i\}\n" /etc/nginx/conf.d/default.conf \
# proxy3
    && sed -i "/server {/i\upstream proxy3 {" /etc/nginx/conf.d/default.conf \
    && sed -i "/server {/i\    server localhost:8002;" /etc/nginx/conf.d/default.conf \
    && sed -i "/server {/i\}\n" /etc/nginx/conf.d/default.conf \
# location /1
    && sed -i "/location \/ {/i\    location /1 {" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        proxy_pass http://proxy1;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        proxy_cache custom_cache;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        proxy_cache_valid any 10m;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        add_header X-Proxy-Cache \$upstream_cache_status;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\    }\n" /etc/nginx/conf.d/default.conf \
# location /2
    && sed -i "/location \/ {/i\    location /2 {" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        proxy_pass http://proxy2;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        proxy_cache custom_cache;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        proxy_cache_valid any 10m;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        add_header X-Proxy-Cache \$upstream_cache_status;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\    }\n" /etc/nginx/conf.d/default.conf \
# location /3
    && sed -i "/location \/ {/i\    location /3 {" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        proxy_pass http://proxy3;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        proxy_cache custom_cache;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        proxy_cache_valid any 10m;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\        add_header X-Proxy-Cache \$upstream_cache_status;" /etc/nginx/conf.d/default.conf \
    && sed -i "/location \/ {/i\    }\n" /etc/nginx/conf.d/default.conf

CMD ["/bin/sh", "-c", "/root/run_8000-2.sh;"]
